package com.cd2cd.code_template.configuration.security;

import com.alibaba.fastjson.JSONObject;
import com.cd2cd.code_template.configuration.security.loginUserService.LoadUserService;
import com.cd2cd.code_template.util.AppCtxHelper;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

@Component
public class MyUserDetailsService implements LoadUserService {

    /**
     * @param rawJson：{username,type}
     * @param <T>
     * @return
     */
    @SneakyThrows
    public <T extends TokenUser> T loadUserByUsername(String rawJson) {

        /**
         * 调取不同的用户信息
         */
        JSONObject obj = JSONObject.parseObject(rawJson);
        String type = obj.getString("type"); // interface class Type

        String username = obj.getString("username");
        LoginTypeUser loginTypeUser = AppCtxHelper.getBean(LoadUserService.getClassByName(type));

        T t = (T) loginTypeUser.loadUserByUsername(username);

        return t;
    }

}
